import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'
import qs from 'qs'
const machinestatusUrl = CommonConfig.machinestatusUrl
const planningUrl = CommonConfig.planningUrl + '/v1'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}
/**
 * 启用/禁用状态
 * param enable id
 * @returns {Promise<unknown>} promise
 */
export function changeCauseEnable (query) {
  const url = planningUrl + '/bad_causes:enable'

  const params = {
    enable: query.enable,
    ids: typeof query.id !== 'object' ? [query.id] : query.id
  }
  return axiosInstance.post(url, params).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 查询所有原因组
 * 机台报表查询停机原因时传入false,不需要过滤
 */
export function getCauseTypeList (causeType, isFilter) {
  const reqData = {
    params: {
      causeType,
      isFilter: !isFilter
    }
  }
  const url = causeType === 3 ? `${machinestatusUrl}/v1/stop_cause_type:selectBatch` : `${planningUrl}/bad_cause_groups`
  return axiosInstance.get(url, reqData).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 删除原因
 */
export function delCause (query) {
  const reqData = {
    params: {
      ids: query.id || undefined
    },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  const url = planningUrl + '/bad_causes:batchDelete'
  return axiosInstance.delete(url, reqData).then((res) => {
    return Promise.resolve((res))
  })
}
// 编辑原因
export function editCauseItem (query) {
  const params = {
    code: query.typeCode || undefined,
    id: query.id || undefined,
    label: query.typeName || undefined,
    gid: query.gid || undefined
  }
  const url = planningUrl + '/bad_causes'
  return axiosInstance.put(url, params).then((res) => {
    return Promise.resolve((res))
  })
}
// 编辑原因类型
export function editCauseType (query) {
  const params = {
    label: query.label || undefined,
    id: query.code || undefined,
    causeType: query.causeType || undefined
  }
  const url = planningUrl + '/bad_cause_groups'
  return axiosInstance.put(url, params).then((res) => {
    return Promise.resolve((res))
  })
}
/**
 * 删除原因类型
 */

export function delCauseType (query) {
  const url = planningUrl + `/bad_cause_groups/${query.id}`
  return axiosInstance.delete(url).then((res) => {
    return Promise.resolve((res))
  })
}
/**
 * 根据原因组代码查询原因
 */
export function getCauseItem (query) {
  const reqData = {
    params: {
      causeGroupId: query.id || undefined,
      showDisable: query.showDisable
    }
  }
  const url = planningUrl + '/bad_causes'
  return axiosInstance.get(url, reqData).then((res) => {
    return Promise.resolve((res))
  })
}
/**
 * 根据原因组代码查询停机原因
 */
export function getCauseItemNew (query) {
  const reqData = {
    params: {
      causeGroupId: query.id || undefined,
      showDisable: query.showDisable
    }
  }
  const url = machinestatusUrl + '/v1/stop_cause_item'
  return axiosInstance.get(url, reqData).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 增加原因
 */

export function addCauseItem (query) {
  const config = {
    params: query,
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  const url = planningUrl + '/bad_causes:batchCreate'
  return axiosInstance.post(url, config.params).then((res) => {
    return Promise.resolve((res))
  })
}
// 新增原因类型
export function addCauseType (query) {
  const params = {
    label: query.label || undefined,
    causeType: query.causeType || undefined
  }
  const url = planningUrl + '/bad_cause_groups'
  return axiosInstance.post(url, params).then((res) => {
    return Promise.resolve((res))
  })
}
